
package parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Tec;

import org.jdom.CDATA;
import org.jdom.Element;

public class MascaraFactory
{

   public static Map<Integer, Map<String, String>> comandos = new HashMap<Integer, Map<String, String>>();
   static
   {

      Map<String, String> tropico = new HashMap<String, String>();
      tropico.put("Criar", "C");
      tropico.put("Modificar", "M");
      tropico.put("Suprimir", "S");
      comandos.put(Tec.TROPICO, tropico);

      Map<String, String> ewsd = new HashMap<String, String>();
      ewsd.put("can", "CAN");
      ewsd.put("cr", "CR");
      ewsd.put("mod", "MOD");
      ewsd.put("entr", "ENTR");
      ewsd.put("conf", "CONF");

      comandos.put(Tec.EWSD, ewsd);

   }

   private static String criaComando(int tec, String acao, String tabela)
   {

      String retorno = comandos.get(tec).get(acao);
      switch (tec)
      {
         case Tec.TROPICO:
            retorno += tabela.substring(1);
            break;
         case Tec.EWSD:
            retorno += tabela;
            break;
         case Tec.AXE:
            break;
         default:
            break;
      }

      return retorno.toUpperCase();
   }

   private static String criaComando(int tec, String acao)
   {
      String retorno = comandos.get(tec).get(acao);
      return retorno.toUpperCase();
   }

   public static Element createSaida(int tec, String acao, String tabela, String[] chaves, String[] params)
   {
      Element mascaraSaida = new Element("mascaraSaida");

      List<String> part = new ArrayList<String>();

      for (String chave : chaves)
      {
         part.add(chave.toUpperCase() + "=" + chave.toLowerCase());
      }

      for (String param : params)
      {
         param = Parser.split(param)[1];
         part.add(param.toUpperCase() + "=" + param.toLowerCase());
      }

      String mascara =
         String.format("<%s:%s", criaComando(tec, acao, tabela), part.toString().replace("[", "").replace("]", ";"));

      mascaraSaida.addContent(new CDATA(mascara));

      return mascaraSaida;
   }

   public static Element createRollback(int tec, String acao, String tabela, String[] chaves, String[] params)
   {
      Element mascaraRollback = new Element("mascaraRollback");

      List<String> part = new ArrayList<String>();

      for (String chave : chaves)
      {
         part.add(chave.toUpperCase() + "=" + chave.toLowerCase());
      }

      String prefixo = "";
      String mascara = "";

      if ("ENTR".equalsIgnoreCase(acao) || "CR".equalsIgnoreCase(acao))
      {
         prefixo = "CAN" + tabela.toUpperCase();
         mascara = String.format("<%s:%s", prefixo, part.toString().replace("[", "").replace("]", ";"));
      }
      else if ("MOD".equalsIgnoreCase(acao))
      {
         prefixo = "MOD" + tabela.toUpperCase();
         mascara = String.format("<%s:%s", prefixo, part.toString().replace("[", "").replace("]", ";"));

      }
      else if ("CAN".equalsIgnoreCase(acao))
      {

         prefixo = "";
         mascara = "";

      }
      else
      {

         prefixo = criaComando(tec, acao, tabela);
         mascara = String.format("<%s:%s", prefixo, part.toString().replace("[", "").replace("]", ";"));

      }

      mascaraRollback.addContent(new CDATA(mascara));

      return mascaraRollback;
   }

}
